﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace DoAn
{
    public partial class frmTralai : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
        public frmTralai()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        private void frmTralai_Load(object sender, EventArgs e)
        {
            //conn.Open();
            cmd = new SqlCommand("SELECT * FROM tralai", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "tralai");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbmtl.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            dtngaytralai.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbmhd.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            tbmkh.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            tbtenkh.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            tbsotienvay.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            cbhtv.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            cbMuclai.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            tbtienlai.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbmtl.Text == "" || dtngaytralai.Text == "" || tbmhd.Text == "" || tbmkh.Text == "" || tbtenkh.Text == "" || tbsotienvay.Text == "" || cbhtv.Text == "" || cbMuclai.Text == "" || tbtienlai.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    tbmtl.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * from tralai", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tralai");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["matralai"] = tbmtl.Text;
                    newRow["ngaydonglai"] = dtngaytralai.Text;
                    newRow["mahd"] = tbmhd.Text;
                    newRow["makh"] = tbmkh.Text;
                    newRow["tenkhachhang"] = tbtenkh.Text;
                    newRow["sotienvay"] = tbsotienvay.Text;
                    newRow["htvten"] = cbhtv.Text;
                    newRow["tllmuclai"] = cbMuclai.Text;
                    newRow["tienlai"] = tbtienlai.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "tralai");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                tbmtl.Focus();

            } 
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "tralai");
            MessageBox.Show("Xóa thành công");
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void bttimkiemmkh_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemmkh.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemmkh.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tralai where makh  = '" + tbtimkiemmkh.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tralai");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bttimkiemmhd_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiemmhd.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiemmhd.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM tralai where mahd  = '" + tbtimkiemmhd.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "tralai");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
             try 
             {if (tbmtl.Text == "")
                {
                    MessageBox.Show("Bắt đầu sửa", "Thông báo!");
                    tbmtl.Focus();
                    return;
                }
                  else
                {
            String matralai = tbmtl.Text;
            String ngaydonglai = dtngaytralai.Value.ToString() ;
            String mahd = tbmhd.Text;
            String makh = tbmkh.Text;
            String tenkhachhang = tbtenkh.Text;
            String sotienvay = tbsotienvay.Text;
            String htvten = cbhtv.Text;
            String tllmuclai = cbMuclai.Text;
            String tienlai = tbtienlai.Text;

            int matl = Int32.Parse(matralai);
            int mahd1 = Int32.Parse(mahd);
            int makh1 = Int32.Parse(makh);
            int sotienvay1 = Int32.Parse(sotienvay);
            int tienlai1 = Int32.Parse(tienlai);
            conn.Open();
            cmd = new SqlCommand("Update tralai set ngaydonglai='" + ngaydonglai + "', mahd = " + mahd1 + ",makh= " + makh1 + ",tenkhachhang = '" +tenkhachhang + "',sotienvay = " + sotienvay1 + ",htvten = '" + htvten + "', tllmuclai = '" + tllmuclai + "',tienlai = " + tienlai1 + " where matralai=" + matl + "", conn);
            da = new SqlDataAdapter(cmd);
            int i = cmd.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds, "tralai");
            cmb = new SqlCommandBuilder(da);
            //dataGridView1.DataSource = ds.Tables[0];
            // this.frmHopdongvayvon_Load();
            //this.LoadForm();
            this.frmTralai_Load(sender, e);
            MessageBox.Show("Sửa thành công");
            conn.Close();
                }
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
                 MessageBox.Show(" thành công");
             }
        }
    }
}
